﻿using StripeDemo.Models;
using StripeDemo.Utils;
using StripeNET;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace StripeDemo.Controllers
{

    public class PaymentController : Controller
    {
        private static Customer cacheCustomer;
        //
        // GET: /Payment/
        public ActionResult Index()
        {
            ViewBag.Msg = "Obtain client payment method";
            cacheCustomer = null;
            return View();
        }

        public ActionResult ListAll()
        {
            ViewBag.cusID = cacheCustomer.CustomerId;
            List<Cards> listCard = new List<Cards>();
            StripeCardService cardService = new StripeCardService();
            try
            {
                IEnumerable<StripeCard> response = cardService.List(cacheCustomer.CustomerId);
                Parallel.ForEach(response, item =>
                {
                    if (null != item)
                    {
                        listCard.Add(Util.convertToCards(item, cacheCustomer.DefaultCard));
                    }
                });
            }
            catch (StripeException exception)
            {
                ViewBag.Msg = exception.StripeError.Message;
                return View("Index");
            }
            return View("ListAllCard", listCard);
        }

        [HttpPost]
        public ActionResult ListAllCard()
        {
            if (HttpContext.Request.RequestType == "POST")
            {
                string ExtraCustomerInfo = Request.Form["ExtraCustomerInfo"].ToString();
                try
                {
                    Customer customer = Util.getCustomerByExtraInfo(ExtraCustomerInfo);
                    if (customer == null)
                    {
                        ViewBag.Msg = "Extra Customer not exist";
                        return View("Index");
                    }
                    cacheCustomer = customer;
                }
                catch (StripeException e)
                {
                    ViewBag.Msg = e.StripeError.Message;
                    return View("Index");
                }
            }
            return RedirectToAction("ListAll", "Payment");
        }

        public ActionResult Default(string id)
        {
            StripeCustomerService stripeCustomerService = new StripeCustomerService();
            StripeCustomer updateCustomer = stripeCustomerService.Get(cacheCustomer.CustomerId);
            StripeCustomerUpdateOptions update = new StripeCustomerUpdateOptions();
            update.DefaultSource = id;
            updateCustomer = stripeCustomerService.Update(cacheCustomer.CustomerId, update);
            cacheCustomer = Util.convertToCustomer(updateCustomer);
            return RedirectToAction("ListAll");
        }

        public ActionResult Detail(string id)
        {
            var cardService = new StripeCardService();
            StripeCard stripeCard = cardService.Get(cacheCustomer.CustomerId, id);
            ViewBag.MsgText = "Edit payment method";
            return View(Util.convertToCards(stripeCard));
        }

        [HttpPost]
        public ActionResult Save(string CardId)
        {
            StripeCardService cardService = new StripeCardService();
            StripeCard stripeCard = cardService.Get(cacheCustomer.CustomerId, CardId);
            string exprireyear = Request.Form[Constant.EXPRIRE_YEAR].ToString();
            string expriremonth = Request.Form[Constant.EXPRIRE_MONTH].ToString();
            StripeCardUpdateOptions cardUpdate = new StripeCardUpdateOptions();
            cardUpdate.ExpirationMonth = expriremonth;
            cardUpdate.ExpirationYear = exprireyear;
            cardUpdate.Name = stripeCard.Name;
            stripeCard = cardService.Update(cacheCustomer.CustomerId, CardId, cardUpdate);
            ViewBag.MsgText = "Edit payment method success!";
            return View("Detail", Util.convertToCards(stripeCard));
        }

        public ActionResult CreateCard()
        {
            return RedirectToAction("Index", "Cards");
        }

        // POST: /Customer/CreateCharge
        [HttpPost]
        public ActionResult CreateCharge()
        {
            if (HttpContext.Request.RequestType == "POST")
            {
                var myCharge = new StripeChargeCreateOptions();

                myCharge.Amount = Int32.Parse(Request.Form["Amount"]);
                //myCharge.Currency = Request.Form["Currency"].ToString();
                myCharge.Description = Request.Form["Description"].ToString();
                myCharge.CustomerId = cacheCustomer.CustomerId;
                Dictionary<string, string> metadata = new Dictionary<string, string>();
                metadata.Add(Constant.EXTRA_TRANSACTION_INFO, Request.Form["ExtraTransactionInfo"].ToString());
                myCharge.Metadata = metadata;
                var chargeService = new StripeChargeService();
                StripeCharge stripeCharge = chargeService.Create(myCharge);
            }
            return View();
        }

    }
}